package util;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * A class that represents a Hashed Password
 * @author Sean Freitag
 *
 */
public class HashedPassword implements Serializable{
	
	/**
	 * Serialized
	 */
	private static final long serialVersionUID = 3700020714176568408L;
	/**
	 * The hashed version of the password
	 */
	private String hashedPassword;
	
	/**
	 * Creates and instance of a HashedPassword to initialize a password
	 * @param password
	 * 		The password that needs to be hashed
	 */
	public HashedPassword(String password){
		try {
		if(password == null)
			password = "";
		
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		md.update(password.getBytes());
		hashedPassword = new String(md.digest());
		} catch (NoSuchAlgorithmException e){
			System.exit(-1);
		}
	}

	/** (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return hashedPassword;
	}

	/** (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		HashedPassword other = (HashedPassword) obj;
		if (hashedPassword == null) {
			if (other.hashedPassword != null)
				return false;
		} else if (!hashedPassword.equals(other.hashedPassword))
			return false;
		return true;
	}

}
